package practica3as;

import entities.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class SceneController implements MouseListener, ActionListener {

    private Renderer renderer;
    private AnimationController animationController;
    private int currentPhase;
    private PhaseLoader phaseLoader;

    public SceneController(Principal principal) {

        Composite scene = new Composite();

        Ball bola = new Ball(100, 100, 1, 1);
        Boundary boundary = new Boundary(499, 399);
        Brick brick1 = new Brick(40, 30, 50, 25);
        Brick brick2 = new Brick(400, 30, 50, 25);
        Brick brick3 = new Brick(40, 300, 50, 25);
        Brick brick4 = new Brick(400, 300, 50, 25);

        scene.add(bola);
        scene.add(boundary);
        scene.add(brick1);
        scene.add(brick2);
        scene.add(brick3);
        scene.add(brick4);

        animationController = new AnimationController(scene);
        animationController.addBall(bola);
        renderer = new Renderer(scene, principal);

        currentPhase = 1;
        
        phaseLoader = new PhaseConnection();
    }

    public void execute() {
        animationController.start();
        renderer.start();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Ball ball = new Ball(e.getX(), e.getY(), 1, 1);
        animationController.addBall(ball);
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            Thread.sleep(5000);
        } catch (Exception ex) {
        }
        switch (e.getActionCommand()) {
            case "Finish": {
                break;
            }
            case "Next": {
                Composite c = (Composite) phaseLoader.loadPhase(currentPhase);
                currentPhase++;
                animationController.setScene(c);
                renderer.setScene(c);
                break;
            }
            default: {
            }
        }
    }
}